package tt.com.deadcold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tt.com.common.enums.Status;
import tt.com.common.model.DeadColdRoom;
import tt.com.common.util.CommonResult;
import tt.com.common.util.PageUtil;
import tt.com.deadcold.mapper.DeadColdMapper;
import tt.com.deadcold.mapper.colddeadMapper;
import tt.com.deadcold.service.DeadColdService;

import java.util.List;

@Service
public class DeadColdImpl extends ServiceImpl<colddeadMapper,DeadColdRoom> implements DeadColdService {
    @Override
    public CommonResult showDeadCold(PageUtil pageUtil) {
        IPage<DeadColdRoom> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadColdRoom> queryWrapper = new QueryWrapper<>();
        IPage<DeadColdRoom> page1 = baseMapper.selectPage(page,queryWrapper);
        pageUtil.setData(page1.getTotal());
        if (pageUtil==null&&pageUtil.equals("")){
            return CommonResult.buildR(Status.DEFAULT,"数据库内容为空");
        }else
            return CommonResult.buildR(Status.OK,"显示成功",pageUtil);
    }

    @Override
    public CommonResult deleteDeadcold(DeadColdRoom deadColdRoom) {
        CommonResult result = null;
        DeadColdRoom deadColdRoom1 = baseMapper.selectById(deadColdRoom.getId());
        if (deadColdRoom1==null){
            return CommonResult.buildR(Status.DEFAULT,"没有这条数据，无法删除");
        }else if (deadColdRoom.getDataStatus()==1){
            deadColdRoom.setDataStatus(0);
        }else {
            return CommonResult.buildR(Status.OK,"该数据已经删除");
        }
        if (baseMapper.updateById(deadColdRoom)==1){
            return CommonResult.buildR(Status.OK,"该条数据已删除");
        }else {
            return CommonResult.buildR(Status.DEFAULT,"删除失败");
        }
    }

    @Override
    public CommonResult updateDeadcold(DeadColdRoom deadColdRoom) {
        if (deadColdRoom==null){
            return CommonResult.buildR(Status.DEFAULT,"没有该数据，无法修改");
        }else
            baseMapper.updateById(deadColdRoom);
        return CommonResult.buildR(Status.OK,"修改成功");
    }

    @Override
    public CommonResult refreshDeadcold(PageUtil pageUtil) {
        List<DeadColdRoom> list = baseMapper.selectList(null);
        pageUtil.setPageSize(list.size());
        IPage<DeadColdRoom> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadColdRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("data_status",0);
        IPage<DeadColdRoom> deadcoldroom = baseMapper.selectPage(page,queryWrapper);
        pageUtil.setData(deadcoldroom);
        return CommonResult.buildR(Status.OK,"刷新成功",pageUtil);
    }

    @Override
    public CommonResult selectDeadcold(DeadColdRoom deadColdRoom, PageUtil pageUtil) {
        List<DeadColdRoom> list = baseMapper.selectList(null);
        IPage<DeadColdRoom> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadColdRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",deadColdRoom.getId());
        IPage<DeadColdRoom> page1 = baseMapper.selectPage(page,queryWrapper);
        if (page1==null){
            return CommonResult.buildR(Status.DEFAULT,"查询结果为空");
        }else {
            return CommonResult.buildR(Status.OK,"查询成功，已返回查询结果",page1);
        }
    }
}
